package com.atguigu.yygh.hosp.testmongo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/mongo1")
public class TestMongo1 {
    @Autowired
    private MongoTemplate mongoTemplate;
    //添加
    @GetMapping("create")
    public void createUser() {
        User user=new User();
        user.setName("test");
        user.setAge(21);
        user.setEmail("1643532388@qq.com");
        User save = mongoTemplate.save(user);
        System.out.println(save);
    }
    //查询所有
    @GetMapping("findAll")
    public void findUser(){
        List<User> all = mongoTemplate.findAll(User.class);
        for (User user : all) {
            System.out.println(user);
        }
    }
    @GetMapping("findId")
    public void getById() {
        User byId = mongoTemplate.findById("60fa987b8124bf51fa51d219", User.class);
        System.out.println(byId);
    }
    @GetMapping("findUser")
    public void findUserList() {
        Query query=new Query();
        query.addCriteria(Criteria.where("name").is("test").and("age").is(21));
        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println(users);
    }
    @GetMapping("findLike")
    public void findUsersLikeName() {
        String name = "est";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query=new Query(Criteria.where("name").regex(pattern));
        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println(users);
    }
    @GetMapping("findPage")
    public void findUsersPage(){
        String name = "est";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query =new Query();
        query.addCriteria(Criteria.where("name").regex(pattern));
        int pageNo=1;
        int pageSize = 10;
        //符合条件的总记录数
        int count = (int)mongoTemplate.count(query, User.class);
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        Map<String,Object> page =new HashMap<>();
        page.put("total",count);
        page.put("list",users);
        System.out.println(page);

    }
    @GetMapping("update")
    public void updateUser() {
        User user = mongoTemplate.findById("60fa987b8124bf51fa51d219", User.class);
        user.setName("lucy");
        user.setAge(23);
        Query query=new Query(Criteria.where("_id").is(user.getId()));
        Update update =new Update();
        update.set("name",user.getName());
        update.set("age",user.getAge());
        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        System.out.println(upsert);
    }
    //删除操作
    @GetMapping("delete")
    public void delete() {
        Query query=new Query(Criteria.where("name").is("test"));
        DeleteResult remove = mongoTemplate.remove(query, User.class);
        long deletedCount = remove.getDeletedCount();
        System.out.println(deletedCount);
    }






}
